package cn.client.ui.components

import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cn.client.data.remoteData.bean.DateUtils
import coil.compose.AsyncImage
import kotlinx.coroutines.launch

/**
 * 当前用户信息的卡片
 */
@Composable
fun ThisUserInfoCard(
    //引入对应的用户信息
    avatarURL: String,
    backGroundURL: String,
    userName: String,
    email: String?,
    birthday: Long?,
    area: String?
) {
    val selectedDateText = remember { mutableStateOf<String?>(null) }
    val scope = rememberCoroutineScope()

    scope.launch {
        birthday?.let { it1 ->
            val dateString = DateUtils.dateToStr(it1)
            // 更新状态以反映转换后的日期字符串
            selectedDateText.value = dateString
        }
    }

    Box(
        modifier = Modifier
            .height(380.dp)
            .fillMaxWidth()
    ) {
        Box(
            modifier = Modifier
                .height(230.dp)
                .fillMaxWidth()
        ) {
            // 背景图
            AsyncImage(
                model = backGroundURL,
                contentDescription = null,
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                contentScale = ContentScale.Crop
            )
        }
        // 基本信息
        Box(
            modifier = Modifier
                .padding(top = 210.dp)
                .height(165.dp)
                .fillMaxWidth()
        ) {
            Card(
                modifier = Modifier
                    .height(160.dp)
                    .fillMaxWidth()
                    .padding(5.dp)
            ) {
                Spacer(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(15.dp)
                )
                // 用户名
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = userName,
                        fontSize = 25.sp,
                        fontWeight = FontWeight.Bold,
                        modifier = Modifier.padding(start = 110.dp)
                    )
                }
                Spacer(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(15.dp)
                )
                // 用户邮箱
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = email!!,
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 30.dp)
                    )
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(5.dp)
                ) {
                    Text(
                        text = "生日：${selectedDateText.value ?: ""}",
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 30.dp)
                    )

                    Text(
                        text = "地区：$area",
                        fontSize = 12.sp,
                        modifier = Modifier.padding(start = 130.dp)
                    )
                }
            }
        }
        // 头像
        Box(
            modifier = Modifier
                .padding(top = 200.dp, start = 30.dp)
                .height(180.dp)
                .fillMaxWidth()
        ) {
            AsyncImage(
                model = avatarURL,
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape)
                    .border(2.dp, MaterialTheme.colorScheme.primary, CircleShape)

            )
        }

    }
}